// pages/order/newOrder/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardList: [],
    item: {},
    url:'https://erp.yilestar.com/',
    date: '请选择',
    message:"",
    price:0,
    show: false,
    url: "https://erp.yilestar.com/",
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },

  },

  onLoad(options) {
    this.getData()
  },

  onShow() {

  },

// 提交数据
  onSubmit() {
    const self = this;
    const url = "https://erp.yilestar.com/weapp/Orders/submit";
    const item = self.data.item;
    const data = {
      tokenId: '5d9df059c27602f5a80e2176d671f9cf',
      custId: wx.getStorageSync('custId'),
      address: item.ADDR, // 客户地址
      custTel: item.TEL, // TODO: 客户电话，这个需要你去添加接收参数
      deliverType: 0, // 送货上门，默认为0
      isInvoice: 0, // 发票，默认为0
      deliveryTime:self.data.date, // 交货日期
      remark: self.data.message, // 备注
    }
    if (data.deliveryTime == '请选择'){
      wx.showToast({
        title: '请选择日期',
        icon: 'none',
      })
      return
    }
    wx.request({
      url,
      data,
      method: 'post',
      success: (res) => {
        if (res.data.status == -1){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
          return
        }
        setTimeout(() => {
          wx.redirectTo({
            url: `../orderCompleted/index?id=${res.data.data}`,
          })
        }, 500)
      },
    })
  },

  
  // 获取清单数据
  getData() {
    const self = this;
    const cardList = wx.getStorageSync('cartList');
    let price = self.data.price
    // todo:因为你接口没有返回商品图片，所以我这里暂时用原来的图片做展示用
    cardList.map(item=>{
      price += item.shopPrice * item.cartNum
    })
    self.getCustData();
    self.setData({
      cardList, price:price*100
    })

  },
  // 获取用户信息
  getCustData() {
    const self = this;
    const url = "https://erp.yilestar.com/weapp/customer/getCustDetail";
    const data = {
      tokenId: '5d9df059c27602f5a80e2176d671f9cf',
      id: wx.getStorageSync('custId')
    }
    var datas;
    wx.request({
      url,
      data,
      method: 'post',
      success: (res) => {
        console.log(res.data)
        self.setData({
          item: res.data.data
        })
      }
    })
  },

  onInput(event) {
    const datas = new Date(event.detail);
    const year = datas.getFullYear();
    const month = datas.getMonth() + Number(1);
    const day = datas.getDay();
    const hours = datas.getHours();
    const minutes = datas.getMinutes();
    const seconds = datas.getSeconds();
    const date = year + '-' + month + '-' + day 
    this.setData({
      currentDate: event.detail,
      date
    });
  },

  confirmDate() {
    this.setData({
      show: false
    })
  },

  cancelDate() {
    this.setData({
      show: false
    })
  },
  showDate() {
    this.setData({
      show: true
    })
  },
  onRemark(e){
    this.setData({
      message: e.detail
    })
  }
})